package com.example.jack.community.persenter.me;

import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.example.jack.community.data.loginbean.RegisterStatus;
import com.example.jack.community.data.loginbean.RegisterVer;
import com.example.jack.community.model.me.Res_PassView;
import com.example.jack.community.view.login.util.Md5;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * Created by TulinTonJie on 2017/11/25.
 */

public class Res_PassPresenterImp implements Res_PassPresenter {

    //使用Handler进行耗时时操作
    private Handler handler = new Handler();
    //Log
    private static final String TAG = "TonJie";
    //passcode字符串
    private static String passcode = null;
    //View层
    private Res_PassView res_PassView;


    @Override
    public void ResetNum(final String oldpass, final String newpass_a, final String newpass_b, final String phonum) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!TextUtils.isEmpty(oldpass)  //旧密码不为空
                        && !TextUtils.isEmpty(newpass_b)  //新密码不为空
                        && !TextUtils.isEmpty(newpass_a)) {//确认密码不为空
                    judgepass(newpass_b, newpass_a, oldpass, phonum);//判断输入的两个密码是否相等
                } else {
                    res_PassView.showToast("请输入要使用的旧密码和新密码");
                }
            }
        }, 600);
    }

    /**
     * 判断输入的两个密码是否相等
     *
     * @param newpass_b 新密码2
     * @param newpass_a 新密码1
     * @param oldpass   旧密码
     * @param phonum    手机号码
     */
    private void judgepass(String newpass_b, String newpass_a, String oldpass, String phonum) {
        if (newpass_b.equals(newpass_a)) {
            resetpasswords(oldpass, newpass_a, phonum);
        } else {
            res_PassView.showToast("很抱歉，您两次输入的账户密码不一样，请重新输入");
        }
    }

    /**
     * 绑定视图
     *
     * @param v
     */
    @Override
    public void attachView(Res_PassView v) {
        this.res_PassView = v;
    }

    /**
     * 解绑视图
     */
    @Override
    public void detachView() {
        res_PassView = null;
    }

    /**
     * 重置密码
     *
     * @param oldpass
     * @param newpass_a
     * @param phnenum
     */
    private void resetpasswords(final String oldpass, final String newpass_a, final String phnenum) {

        String url;
        url = "http://192.168.1.11/change/";
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.d(TAG, "onError: " + id);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //获取返回的Json数据,格式化成为Java实体类reg
                        final Gson reg_Ver = new Gson();
                        RegisterVer reg = reg_Ver.fromJson(response, RegisterVer.class);
                        Md5 md = new Md5();
                        passcode = md.md5(reg.getData()).substring(20, 30);
                        Log.d(TAG, "Res_PassPresenterImp: username:" + phnenum + "    newpassword:" + newpass_a + "  telephone:" + phnenum + "   passcode:"
                                + passcode + "   ChangeDataType:2" + " oldpass:" + oldpass);
                        final String pas_new = md.md5(newpass_a);
                        final String pas_old = md.md5(oldpass);
                        OkHttpUtils
                                .post()
                                .url("http://192.168.1.11/change/")
                                .addParams("ChangeDataType", "1")
                                .addParams("username", phnenum)
                                .addParams("newpassword", pas_new)
                                .addParams("oldpassword", pas_old)
                                .addParams("telephone", "0")
                                .addParams("passcode", passcode)
                                .addParams("token", "0")
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Call call, Exception e, int id) {

                                    }

                                    @Override
                                    public void onResponse(String response, int id) {
                                        Log.d(TAG, "onResponse: 进行post请求" + response);
                                        Gson gson = new Gson();

                                        RegisterStatus shopInfo = gson.fromJson(response, RegisterStatus.class);


                                        if (shopInfo.getStatus().equals("0")) {
                                            res_PassView.resetSuccess("Reset the success");
                                        }
                                        if (shopInfo.getStatus().equals("1")) {
                                            res_PassView.resetFail("原密码错误");
                                        }
                                    }
                                });
                    }
                });
    }

}
